<template>
  <Dialog :title="title" v-model="visible" top="10vh">
    <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px">
      <el-form-item v-if="showUserSelecte" label="群组可见" prop="userIds">
        <UserSelectButton v-model="selectedUsers" />
      </el-form-item>
      <el-form-item v-if="showUserSelecte" label="群组可编辑" prop="editUserIds">
        <UserSelectButton v-model="selectedEditUser" />
      </el-form-item>
      <el-form-item v-if="formType !== 'permission'" label="名称" prop="name">
        <el-input v-model="formData.name" autocomplete="off" maxlength="20" show-word-limit/>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :loading="formLoading">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ResourceFileApi } from '@/api/school/resource'
const props: any = defineProps<{
  query?: object // 活动ID（主表的关联字段）
}>()
const emit = defineEmits(['success'])
const visible = ref(false)
const formLoading = ref(false)
const formData: any = ref({
  name: '',
  userIds: undefined,
  editUserIds: undefined
})

const formRules = computed(() => {
  // const { rightStatus }: any = props.query
  const rules: any = {
    name: [{ required: true, message: '请输入名称', trigger: 'blur' }]
  }
  // if (rightStatus === 1) {
  //   rules.userIds = [{ required: true, message: '群组可见不能为空', trigger: 'blur' }]
  // }
  return rules
})
const formRef = ref() // 表单 Ref
const selectedUsers = ref<any[]>([])
const selectedEditUser = ref<any[]>([])
const baseData: any = ref(null)
const formType = ref('')
const title = computed(() => {
  return baseData.value?.id
    ? formType.value === 'permission'
      ? '权限设置'
      : '重命名'
    : '新建文件夹'
})
const showUserSelecte = computed(() => {
  return props.query?.rightStatus === 1 && (!baseData.value?.id || formType.value === 'permission')
})
// 人员选择回显
const setUsersList = (usersData: any) => {
  selectedUsers.value = usersData?.userIds?.map((id: string, index: number) => {
    return {
      id,
      nickname: usersData?.userNames[index]
    }
  })
  selectedEditUser.value = usersData?.editUserIds?.map((id: string, index: number) => {
    return {
      id,
      nickname: usersData?.editUserNames[index]
    }
  })
}
// 表单赋值人员id
const setFormUserIds = () => {
  const userIds: any = selectedUsers.value?.map((user: any) => user.id)
  const editUserIds: any = selectedEditUser.value?.map((user: any) => user.id)
  formData.value.userIds = userIds
  formData.value.editUserIds = editUserIds
}
const open = async (data?: any, type?: string) => {
  formType.value = type || ''
  selectedUsers.value = []
  selectedEditUser.value = []
  baseData.value = data || null
  formData.value = data || {
    name: '',
    userIds: undefined,
    editUserIds: undefined
  }
  if (data?.id && props?.query?.rightStatus === 1) {
    try {
      const usersData = await ResourceFileApi.getResourceUsers(data?.id)
      setUsersList(usersData)
    } catch (error) {
      console.log(error)
    }
  }

  visible.value = true
}

const submitForm = async () => {
  setFormUserIds()
  // 校验表单
  await formRef.value.validate()
  try {
    const folderApi = baseData.value
      ? ResourceFileApi.updateResourceFolder
      : ResourceFileApi.createResourceFolder
    const data: any = await folderApi({
      id: baseData.value?.id,
      ...formData.value,
      ...props.query
    })
    if (data > -1) {
      visible.value = false
      ElMessage.success(
        baseData.value
          ? formType.value === 'permission'
            ? '权限设置成功'
            : '重命名成功'
          : '创建成功'
      )
      emit('success')
    }
  } finally {
  }
}
defineExpose({
  open
})
</script>
